package com.bw.demo_lazy_fragment

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.bw.demo_lazy_fragment.adapter.FragmentAdapter
import com.bw.demo_lazy_fragment.ui.FourFragment
import com.bw.demo_lazy_fragment.ui.OneFragment
import com.bw.demo_lazy_fragment.ui.ThreeFragment
import com.bw.demo_lazy_fragment.ui.TwoFragment

/**\
 * 预加载：viewPager有预加载功能，默认提前加载左右各一页
 * 懒加载：只有当前页面可见的时候采取
 */
class MainActivity : AppCompatActivity() {
    private lateinit var viewPagerAdapter: FragmentAdapter
    private lateinit var viewPager: ViewPager
    private val list: MutableList<Fragment> = mutableListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewPager = findViewById(R.id.vp)
        list += OneFragment()
        list += TwoFragment()
        list += ThreeFragment()
        list += FourFragment()
        viewPagerAdapter = FragmentAdapter(list, supportFragmentManager, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
        viewPager.adapter = viewPagerAdapter
    }
}